/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.report.paymentWithdrawLog.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.CommonUtil;
import cn.apestar.common.utils.DateUtils;
import cn.apestar.common.utils.excel.ExportExcel;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.report.paymentWithdrawLog.entity.PageResponse;
import cn.apestar.modules.report.paymentWithdrawLog.entity.PaymentWithdrawLogAuditListRequest;
import cn.apestar.modules.report.paymentWithdrawLog.entity.PaymentWithdrawLogAuditResponse;
import cn.apestar.modules.report.paymentWithdrawLog.service.PaymentWithdrawLogService;

/**
 * 系统公告Controller
 * @author yudw
 * @version 2016-08-20
 */
@Controller
@RequestMapping(value = "${adminPath}/paymentWithdrawLog/paymentWithdrawLog")
public class PaymentWithdrawLogController extends BaseController {

    @Autowired
    PaymentWithdrawLogService paymentWithdrawLogService;

    public int size = 2000;

    @RequestMapping(value = {"list" , ""})
    public String list(PaymentWithdrawLogAuditListRequest log, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (StringUtils.isEmpty(log.getStatus())) {
            log.setStatus("process");
        }
        Page<Object> page = new Page(request, response, 10);
        log.setPage(page.getPageNo());
        log.setSize(size);
        PageResponse pageResponse = paymentWithdrawLogService.list(log);
        page.setList(pageResponse.getItems());
        page.setCount(pageResponse.getTotal_record());
        page.setPageNo(pageResponse.getCurrent_page());
        page.setPageSize(size);
        page.initialize();
        model.addAttribute("page", page);
        model.addAttribute("paymentWithdrawLog", log);
        // return "modules/paymentWithdrawLog/paymentWithdrawLogList";
        return "modules/report/paymentwithdrawlog/list";
    }

    @RequestMapping(value = "/audit")
    public String form(PaymentWithdrawLogAuditListRequest log, Model model, HttpServletRequest request) {
        model.addAttribute("paymentWithdrawLog", log);

        paymentWithdrawLogService.aduit(log);
        return "redirect:" + Global.getAdminPath() + "/paymentWithdrawLog/paymentWithdrawLog?withdraw_type=institution_balance";
    }

    /**
     * 导出用户数据
     * @param user
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public String exportFile(PaymentWithdrawLogAuditListRequest log, HttpServletRequest request, HttpServletResponse response,
        RedirectAttributes redirectAttributes) {

        try {
            String fileName = "提现数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<Object> page = new Page(request, response, 10);
            log.setPage(page.getPageNo());
            log.setSize(page.getPageSize());
            PageResponse pageResponse = paymentWithdrawLogService.list(log);
            List<PaymentWithdrawLogAuditResponse> list = new ArrayList();
            List<Map> listMap = pageResponse.getItems();
            for (int i = 0; i < listMap.size(); i++) {
                Map m = listMap.get(i);
                PaymentWithdrawLogAuditResponse s = new PaymentWithdrawLogAuditResponse();
                s.setAccount(ObjectUtils.toString(m.get("account"), ""));
                s.setNick_name(ObjectUtils.toString(m.get("nick_name"), ""));
                s.setUser_level(CommonUtil.toInteger(m.get("user_level"), 0));
                s.setTransfer_account(ObjectUtils.toString(m.get("transfer_account"), ""));
                s.setTransfer_username(ObjectUtils.toString(m.get("transfer_username"), ""));
                s.setMoney(CommonUtil.getBigDecimal(m.get("money")));
                s.setIs_vip_desc(ObjectUtils.toString(m.get("is_vip_desc"), ""));
                String state = ObjectUtils.toString(m.get("status"), "process");
                if ("back".equals(state)) {
                    s.setStatus_desc("已退回");
                }
                if ("pass_failed".equals(state)) {
                    s.setStatus_desc("未通过");
                }
                if ("process".equals(state)) {
                    s.setStatus_desc("提现中");
                }
                if ("complete".equals(state)) {
                    s.setStatus_desc("完成");
                }
                s.setAccount(ObjectUtils.toString(m.get("account"), ""));
                s.setCreate_date(ObjectUtils.toString(m.get("withdraw_date"), ""));
                list.add(s);
            }
            new ExportExcel("提现数据", PaymentWithdrawLogAuditResponse.class).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出用户失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + adminPath + "/sys/user/list?repage";
    }
}